package com.glory.good_dark.activity;

import android.os.Handler;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.recyclerview.widget.RecyclerView;

import com.glory.good_dark.R;
import com.glory.good_dark.adapter.StrIpMangerAdapter;
import com.glory.good_dark.application.App;
import com.glory.good_dark.entity.StrIDIPEntity;
import com.glory.good_dark.entity.response.BaseEntity;
import com.glory.good_dark.utils.NetUtils;
import com.google.gson.Gson;
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;
import com.google.gson.reflect.TypeToken;
import com.lzy.okgo.model.Response;
import com.pda.platform.ui.ui_pdaplatform.callback.FreeUI_DialogEntityCallBack;
import com.pda.platform.ui.ui_pdaplatform.callback.FreeUI_EntityCallBack;
import com.pda.platform.ui.ui_pdaplatform.entity.FreeUI_AddViewEntity;
import com.pda.platform.ui.ui_pdaplatform.utils_public.FreeApi_StaticMembers;


import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;

public class StripIDMangerActivity extends BaseProjectActivity {

    @BindView(R.id.rvBind)
    RecyclerView rvBind;
    @BindView(R.id.llMain)
    LinearLayout llMain;
    @BindView(R.id.etLot)
    TextView etLot;
    @BindView(R.id.rvUnBind)
    RecyclerView rvUnBind;
    @BindView(R.id.llArrowLayout)
    LinearLayout llArrowLayout;
    @BindView(R.id.ivAddLeft)
    ImageView ivAddLeft;
    @BindView(R.id.ivAddRight)
    ImageView ivAddRight;
    private StrIpMangerAdapter bindAdapter;
    private StrIpMangerAdapter unBindAdapter;
    private boolean change = false;


    @Override
    protected int getLayoutResId() {
        return R.layout.activity_strip_id_manger;
    }

    @Override
    protected void initView() {
        initRecyclerView(rvBind,this);
        initRecyclerView(rvUnBind,this);
        bindAdapter = new StrIpMangerAdapter(null,getSupportFragmentManager());
        unBindAdapter = new StrIpMangerAdapter(null,getSupportFragmentManager());
        rvBind.setAdapter(bindAdapter);
        rvUnBind.setAdapter(unBindAdapter);
    }

    @Override
    protected void initData() {

    }


    @OnClick({R.id.ivAddLeft,R.id.ivAddRight,R.id.etLot,R.id.btnAdd})
    public void onViewClicked(View view) {
        switch (view.getId()){
            case R.id.ivAddLeft:
                List<StrIDIPEntity.StrIDEntity> data = new ArrayList<>();
                for (StrIDIPEntity.StrIDEntity datum : unBindAdapter.getData()) {
                    if (datum.isChecked())
                    data.add(datum);
                }
                if (data.size() <=0){
                    showToast("请先勾选未绑料条列表",false);
                    return;
                }
                bindAdapter.addData(data);
                unBindAdapter.getData().removeAll(data);
                unBindAdapter.notifyDataSetChanged();
                change = true;
                break;
            case R.id.ivAddRight:
                List<StrIDIPEntity.StrIDEntity> bindData = new ArrayList<>();
                for (StrIDIPEntity.StrIDEntity datum : bindAdapter.getData()) {
                    if (datum.isChecked())
                    bindData.add(datum);
                }
                if (bindData.size() <=0){
                    showToast("请先勾选已绑料条列表",false);
                    return;
                }

                unBindAdapter.addData(bindData);
                bindAdapter.getData().removeAll(bindData);
                bindAdapter.notifyDataSetChanged();
                change = true;
                break;
            case R.id.etLot:
                showSpanDialog(10010,"请输入或扫描批次号");
                break;
            case R.id.btnAdd:
                if (etLot.getText().toString().isEmpty()){
                    showToast("请先扫描批次号",false);
                    return;
                }
                showSpanDialog(10020,"请输入或扫描料条ID");
                break;
        }
    }


    @Override
    protected FreeUI_AddViewEntity getBaseEntity() {
        freeUI_titleView.setTitle("StripId管理");
        freeUI_titleView.setRightListener("提交", new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (etLot.getText().toString().isEmpty()){
                    showToast("请先扫描批次号",false);
                    return;
                }

                if (!change){
                    showToast("绑定关系未更改，请确认后提交！",false);
                    return;
                }
                FreeUI_EntityCallBack<BaseEntity<StrIDIPEntity>> callBack = new FreeUI_DialogEntityCallBack<BaseEntity<StrIDIPEntity>>
                        (new TypeToken<BaseEntity<StrIDIPEntity>>() {
                        }.getType(), getSupportFragmentManager(), this) {

                    @Override
                    public void onSuccess
                            (final Response<BaseEntity<StrIDIPEntity>> response) {
                        if (response.body().isSuccess(getApplicationContext(),getSupportFragmentManager())) {
                            showToast("提交成功",true);
                            new Handler().postDelayed(new Runnable() {
                                @Override
                                public void run() {
                                    StripIDMangerActivity.this.finish();
                                }
                            },2500);
                        }
                    }

                    @Override
                    public void onError
                            (Response<BaseEntity<StrIDIPEntity>> response) {
                        super.onError(response);
                        loadError(response.getException(), "PDAPutStripID");
                    }
                };
                JsonObject jsonObject = new JsonObject();
                jsonObject.addProperty("EQP_ID", App.EQPID);
                jsonObject.addProperty("LOT_ID", etLot.getText().toString());
                jsonObject.add("BIND_STRIPS",new JsonParser().parse(new Gson().toJson(bindAdapter.getData())).getAsJsonArray());
//                jsonObject.add("UNBIND_STRIPS",new JsonParser().parse(new Gson().toJson(unBindAdapter.getData())).getAsJsonArray());
                NetUtils.requestPostNet(App.WEB_URL + "/api/PDA", this, callBack, "PDAPutStripID", jsonObject);

            }
        });
        return new FreeUI_AddViewEntity(getIntent().getStringExtra(FreeApi_StaticMembers.SHOW_PLUGIN_KEY), llMain);
    }

    @Override
    protected void EQPReplaceSuccess(int tag, String content) {
        if (tag == 10010){
            //获取料条
            etLot.setText(content);

            FreeUI_EntityCallBack<BaseEntity<StrIDIPEntity>> callBack = new FreeUI_DialogEntityCallBack<BaseEntity<StrIDIPEntity>>
                    (new TypeToken<BaseEntity<StrIDIPEntity>>() {
                    }.getType(), getSupportFragmentManager(), this) {

                @Override
                public void onSuccess
                        (final Response<BaseEntity<StrIDIPEntity>> response) {
                    if (response.body().isSuccess(getApplicationContext(),getSupportFragmentManager())) {
                        StrIDIPEntity body = response.body().getData().getBody();
                        bindAdapter.setNewData(body.getBIND_STRIPS());
                        unBindAdapter.setNewData(body.getUNBIND_STRIPS());
                        llArrowLayout.setVisibility(View.VISIBLE);
                    }
                }

                @Override
                public void onError
                        (Response<BaseEntity<StrIDIPEntity>> response) {
                    super.onError(response);
                    loadError(response.getException(), "PDAGetStripID");
                }
            };
            JsonObject jsonObject = new JsonObject();
            jsonObject.addProperty("EQP_ID", App.EQPID);
            jsonObject.addProperty("LOT_ID", content);
            NetUtils.requestPostNet(App.WEB_URL + "/api/PDA", this, callBack, "PDAGetStripID", jsonObject);
        }else if (tag == 10020){
            StrIDIPEntity.StrIDEntity entity = new StrIDIPEntity.StrIDEntity();
            entity.setSTRIP_ID(content);
            entity.setChecked(false);
            bindAdapter.addData(entity);
            change = true;
        }
    }
}